package com.googlecode.framework.core.encoder.support;

import com.googlecode.framework.core.encoder.MD5TextEncoder;
import com.googlecode.framework.core.encoder.SHATextEncoder;
import com.googlecode.framework.core.encoder.TextEncoder;

/**
 * 默认的密码加密编码器
 * 
 * @author xiayg
 *
 */
public class DefaultPasswordEncoder implements TextEncoder {

    private String salt;

    private TextEncoder shaEncoder;

    private TextEncoder md5Encoder;

    public DefaultPasswordEncoder(String salt) {
        this.salt = salt;
        this.shaEncoder = new SHATextEncoder();
        this.md5Encoder = new MD5TextEncoder();
    }

    /*
     * (non-Javadoc)
     * @see com.googlecode.toolkit.encoder.TextEncoder#encode(java.lang.String)
     */
    @Override
    public String encode(String password) {
        String passwordToUse = password;
        passwordToUse = this.shaEncoder.encode(password);
        passwordToUse = passwordToUse + "#" + this.salt;
        passwordToUse = this.md5Encoder.encode(passwordToUse);
        return passwordToUse;
    }
}
